<?php

namespace app\index\controller;

use app\merch\controller\Main;
use think\Controller;
use think\Db;

class Cash extends Main {

    public function index() {
        $_GPC = input();
        $page = max(intval($_GPC['page']), 1);
        $limit = max(intval($_GPC['limit']), 10);
        $map['member_id'] = $this->merchinfo['id'];
        $db = Db::name('cash_log')->alias('c')->join('member m', 'c.member_id=m.id')->where($map);
        $count = $db->count();
        $data = $db->order('c.createtime DESC')->limit(($page - 1) * $limit, $limit)->select();
        jsonp($data, $count);
    }

    public function tongji() {

        $_GPC = input();
        $cash_log0 = Db::name('cash_log')->where(array('member_id' => $this->merchinfo['id'], 'type' => 0, 'status' => 0))->find();
        $cash_log1 = Db::name('cash_log')->where(array('member_id' => $this->merchinfo['id'], 'type' => 1, 'status' => 0))->find();
        $data = array(
            array('text1' => '累计提现', 'text2' => '累计提现次数', 'cash' => false, 'type' => '', 'num1' => 0, 'num2' => 0),
            array('text1' => '可提现金额', 'text2' => '累计充值', 'cash' => $cash_log0 ? false : true, 'type' => '0', 'num1' => 0, 'num2' => 0),
            array('text1' => '剩余次数', 'text2' => '已使用次数', 'cash' => false, 'type' => '', 'num1' => 0, 'num2' => 0),
            array('text1' => '可结算金额', 'text2' => '累计结算金额', 'cash' => $cash_log1 ? false : true, 'type' => '1', 'num1' => 0, 'num2' => 0)
        );

        $num = Db::name('cash_log')->field('SUM(price) AS price')->where(array('member_id' => $this->merchinfo['id'], 'status' => 1))->find();
        $data[0]['num1'] = '￥' . number_format($num['price'], 2);
        $data[0]['num2'] = Db::name('cash_log')->where(array('member_id' => $this->merchinfo['id'], 'status' => 1))->count();
        $member = Db::name('member')->where(array('id' => $this->merchinfo['id']))->find();
        if ($this->sysset['agent_level'][$member['agent_level']]['price'] > 0 && $this->sysset['agent_level'][$member['agent_level']]['num'] > 0) {
            $unit = $this->sysset['agent_level'][$member['agent_level']]['price'] / $this->sysset['agent_level'][$member['agent_level']]['num'];
        } elseif ($this->sysset['agent_level'][$member['agent_level']]['discount'] > 0) {
            $unit = 1 / $this->sysset['recharge']['basenum'] * $this->sysset['agent_level'][$member['agent_level']]['discount'] / 100;
        } else {
            $unit = $member['credit1'] / $member['nums'];
        }
        $data[1]['num1'] = '￥' . number_format($unit * ($member['nums'] - $member['numing']), 2);
        $data[1]['num2'] = '￥' . $member['credit1'];
        $data[2]['num1'] = $member['nums'] - $member['numing'];
        $data[2]['num2'] = $member['numing'];
        if ($member['isagent'] == 0) {
            unset($data[3]);
        } elseif ($this->sysset['agent_level'][$member['agent_level']]['type'] == 0) {
            $child = Db::name('member')->field('SUM(numing) AS numing')->where(array('id <>' => $this->merchinfo['id'], 'agent_id' => $this->merchinfo['id']))->find();
            $unitc = 1 / $this->sysset['recharge']['basenum'];
            $data[3]['num1'] = '￥' . number_format(($unitc - $unit) * $child['numing'] - $member['credit2'], 2);
            $data[3]['num2'] = '￥' . number_format($member['credit2'], 2);
        } else {
            $cal = Db::name('agent_level')->where(array('level <' => $this->sysset['agent_level'][$this->merchinfo['agent_level']]['level']))->order('level', 'DESC')->find();
            $condition = array('id <>' => $this->merchinfo['id'], 'agent_id <>' => $this->merchinfo['id']);
            if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 1) {
                $condition['province'] = $this->merchinfo['province'];
                $condition['city'] = $this->merchinfo['city'];
                $condition['area'] = $this->merchinfo['area'];
            }
            if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 2) {
                $condition['province'] = $this->merchinfo['province'];
                $condition['city'] = $this->merchinfo['city'];
            }
            if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 3) {
                $condition['province'] = $this->merchinfo['province'];
            }
            $child = Db::name('member')->field('SUM(numing) AS numing')->where($condition)->find();
            if ($cal['price'] > 0 && $cal['num'] > 0) {
                $unitc = $cal['price'] / $cal['num'];
            } elseif ($cal['discount'] > 0) {
                $unitc = 1 / $this->sysset['recharge']['basenum'] * $cal['discount'] / 100;
            } else {
                $unitc = $member['credit1'] / $member['nums'];
            }
            $data[3]['num1'] = ($unitc - $unit) * $child['numing'];
            $child = Db::name('member')->field('SUM(numing) AS numing')->where(array('id <>' => $this->merchinfo['id'], 'agent_id' => $this->merchinfo['id']))->find();
            $unitc = 1 / $this->sysset['recharge']['basenum'];
            $data[3]['num1'] = number_format($data[3]['num1'] + ($unitc - $unit) * $child['numing'] - $member['credit2'], 2);
            $data[3]['num1'] = $data[3]['num1'] == 0 ? '￥0.00' : '￥' . $data[3]['num1'];
            $data[3]['num2'] = '￥' . number_format($member['credit2'], 2);
        }
        json($data);
    }

    public function add() {

        $_GPC = input();
        $member = Db::name('member')->where(array('id' => $this->merchinfo['id']))->find();
        $data = array();
        if ($this->sysset['agent_level'][$member['agent_level']]['price'] > 0 && $this->sysset['agent_level'][$member['agent_level']]['num'] > 0) {
            $unit = $this->sysset['agent_level'][$member['agent_level']]['price'] / $this->sysset['agent_level'][$member['agent_level']]['num'];
        } elseif ($this->sysset['agent_level'][$member['agent_level']]['discount'] > 0) {
            $unit = 1 / $this->sysset['recharge']['basenum'] * $this->sysset['agent_level'][$member['agent_level']]['discount'] / 100;
        } else {
            $unit = $member['credit1'] / $member['nums'];
        }
        if ($_GPC['type'] == '0') {
            $data['price'] = round($unit * ($member['nums'] - $member['numing']), 2);
            $data['num'] = $member['nums'] - $member['numing'];
        } else {
            $cnum = Db::name('cash_log')->field('SUM(num) AS num')->where(array('member_id' => $this->merchinfo['id'], 'type' => 1, 'status' => 1))->find();
            if ($this->sysset['agent_level'][$member['agent_level']]['type'] == 0) {
                $child = Db::name('member')->field('SUM(numing) AS numing')->whereNotIn('id', [$this->merchinfo['id'], 'agent_id' => $this->merchinfo['id']])->find();
                $unitc = 1 / $this->sysset['recharge']['basenum'];
                $data['price'] = round(($unitc - $unit) * $child['numing'] - $member['credit2'], 2);
                $data['num'] = $child['numing'] - $cnum['num'];
            } else {
                $cal = Db::name('agent_level')->where('leve', '<', $this->sysset['agent_level'][$this->merchinfo['agent_level']]['level'])->order('level', 'DESC')->find();
                $condition = [];
                if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 1) {
                    $condition['province'] = $this->merchinfo['province'];
                    $condition['city'] = $this->merchinfo['city'];
                    $condition['area'] = $this->merchinfo['area'];
                }
                if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 2) {
                    $condition['province'] = $this->merchinfo['province'];
                    $condition['city'] = $this->merchinfo['city'];
                }
                if ($this->sysset['agent_level'][$this->merchinfo['agent_level']]['type'] == 3) {
                    $condition['province'] = $this->merchinfo['province'];
                }
                $child = Db::name('member')->field('SUM(numing) AS numing')->where($condition)->whereNotIn('id', [$this->merchinfo['id'], 'agent_id' => $this->merchinfo['id']])->find();
                if ($cal['price'] > 0 && $cal['num'] > 0) {
                    $unitc = $cal['price'] / $cal['num'];
                } elseif ($cal['discount'] > 0) {
                    $unitc = 1 / $this->sysset['recharge']['basenum'] * $cal['discount'] / 100;
                } else {
                    $unitc = $member['credit1'] / $member['nums'];
                }
                $data['price'] = ($unitc - $unit) * $child['numing'];
                $data['num'] = $child['numing'];
                $child = Db::name('member')->field('SUM(numing) AS numing')->whereNotIn('id', [$this->merchinfo['id'], 'agent_id' => $this->merchinfo['id']])->find();
                $unitc = 1 / $this->sysset['recharge']['basenum'];
                $data['price'] = round($data['price'] + ($unitc - $unit) * $child['numing'] - $member['credit2'], 2);
                $data['num'] = $data['num'] + $child['numing'] - $cnum['num'];
            }
        }
        if ($this->request->isPost()) {
            $token = check_formtoken();
            if ($data['price'] <= 0) {
                json(array('token' => $token), 1, '提现金额必须大于零！');
            }
            if ($_GPC['type'] == '0') {
                $cash_log = Db::name('cash_log')->where(array('member_id' => $this->merchinfo['id'], 'type' => 0, 'status' => 0))->get();
            } else {
                $cash_log = Db::name('cash_log')->where(array('member_id' => $this->merchinfo['id'], 'type' => 1, 'status' => 0))->get();
            }
            if ($cash_log) {
                json(array('token' => $token), 1, '重复提现，操作无效！');
            }
            if (session('fans')['agent_id'] > 0) {
                $agent = pdo_get('member', array('id' => session('fans')['agent_id']));
            } else {
                $agent = '';
            }
            $result = pdo_insert('cash_log', array('type' => $_GPC['type'], 'member_id' => $member['id'], 'agent_id' => $member['agent_id'], 'agent' => json_encode($agent), 'province' => $member['province'], 'city' => $member['city'], 'district' => $member['area'], 'price' => $data['price'], 'num' => $data['num'], 'createtime' => time(), 'updatetime' => time(), 'status' => 0, 'desc' => '', 'info' => json_encode($_GPC['data'])));
            if (!empty($result) || $result === 0) {
                json(array('token' => $token));
            } else {
                json(array('token' => $token), 1, '提交失败！');
            }
        }
        jsont($data);
    }

}
